﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Net;

namespace Microsoft.Practices.Mobile.DisconnectedAgent.WebService.ConfigurationCredentialService
{
    public class ConfigurationCredentialService : IWSCredentialService
    {
        internal CredentialDictionary dictionary = new CredentialDictionary();

        internal ConfigurationCredentialService()
        {
        }

        public static ConfigurationCredentialService CreateFromConfiguration()
        {
            return CreateFromConfiguration("Credentials");
        }

        public static ConfigurationCredentialService CreateFromConfiguration(string sectionName)
        {
            ConfigurationCredentialService service = new ConfigurationCredentialService();

            CredentialsSection accounts = (CredentialsSection) Configuration.ConfigurationManager.GetSection(sectionName);

            foreach (AccountElement account in accounts.AccountsCollection)
            {
                service.dictionary.AddCredential(account.AccountName, account.Username, account.Password, account.Domain);
            }
            return service;
        }

        public NetworkCredential GetCredential(string accountName)
        {
            return dictionary.GetCredential(accountName);
        }
    }
}
